package com.etc.service.impl;

import com.etc.dao.CommentOfDao;
import com.etc.pojo.Comment;
import com.etc.pojo.CommentGroup;
import com.etc.pojo.User;
import com.etc.service.CommentOfService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class CommentOfServiceImpl implements CommentOfService {

    @Autowired
    CommentOfDao dao;

    /**
     * 增加评论
     * @param userId
     * @param videoId
     * @param commentContent
     * @return
     */
    @Override
    public boolean addComment(String userId,int videoId,String commentContent) {
        User user = new User(userId);
        Comment comment = new Comment(user, videoId, commentContent);
        return dao.insertComment(comment)>0;
    }

    /**
     * 回复
     * @param userId
     * @param videoId
     * @param commentContent
     * @param replyId
     * @return
     */
    @Override
    public boolean addCommentReply(String userId,int videoId,String commentContent,int replyId) {
        User user = new User(userId);
        Comment comment = new Comment(user, videoId, commentContent,replyId);
        return dao.insertReplyComment(comment)>0;
    }

    /**
     * 删除
     * @param commentId
     * @return
     */
    @Override
    public boolean deleteCommentByCommentId(int commentId) {
        return dao.deleteCommentByCommentId(commentId)>0;
    }

    /**
     * 根据视频编号查询评论
     * @param videoId
     * @return
     */
    @Override
    public List<CommentGroup> getCommentByVideoId(int videoId) {
        List<Comment> comments = dao.queryCommentByVideoId(videoId);
        List<CommentGroup> list = new ArrayList<>();
        for (Comment comment:comments){
            Comment replyComment = dao.queryCommentById(comment.getReplyId());

            CommentGroup commentGroup = new CommentGroup(comment,replyComment);
            list.add(commentGroup);
        }

        return list;
    }

    /**
     * 根据评论编号查询评论
     * @param commentId
     * @return
     */
    @Override
    public Comment getCommentById(int commentId) {
        return dao.queryCommentById(commentId);
    }
}
